package vectors;

import util.Vector;

import java.io.Serializable;
import java.util.Iterator;
import java.util.NoSuchElementException;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//package javaapplication1;

/**
 * @author s32465
 */
public class ArrayVector implements Vector, Serializable {
    private float vector[];

    public ArrayVector(int length) {
        vector = new float[length];
        for (int i = 0; i < vector.length; i++) {
            vector[i] = 0;
        }
    }

    public ArrayVector(Vector vector) {
        this.vector = new float[vector.length()];
        for (int i = 0; i < vector.length(); i++) {
            this.vector[i] = vector.getElement(i);
        }
    }

    public ArrayVector() {
        //To change body of created methods use File | Settings | File Templates.
    }

    public float getElement(int index) {
        //   if (elem>0 || elem<vector.length){
        return vector[index];
        //}
        // else return
        //      System.out.println("Wrong index");


    }

    public void setElement(int index, float elem) {
        //   if (elem>0 || elem<vector.length){
        vector[index] = elem;
    }

    public int length() {
        return vector.length;
    }


    public float min() {
        float min = vector[0];
        for (int i = 0; i < vector.length; i++) {
            if (min > vector[i]) {
                min = vector[i];
            }
        }
        return min;
    }

    public float max() {
        float max = vector[0];
        for (int i = 0; i < vector.length; i++) {
            if (max < vector[i]) {
                max = vector[i];
            }
        }
        return max;
    }

    public void sort() {
        for (int i = 0; i < vector.length - 1; i++) {
            for (int j = i + 1; j < vector.length; j++) {
                if (vector[i] > vector[j]) {
                    float temp = vector[j];
                    vector[j] = vector[i];
                    vector[i] = temp;

                }
            }
        }
    }

    public void print() {
        for (int i = 0; i < vector.length; i++) {
            System.out.println(vector[i]);
        }
    }

    public double evklidNorm() {
        float sum = 0;
        for (int i = 0; i < vector.length; i++) {
            sum += vector[i] * vector[i];
        }
        return Math.sqrt(sum);
    }

    public Iterator iterator() {
        return new iterator(this);  //To change body of implemented methods use File | Settings | File Templates.
    }

    class iterator implements Iterator {
        private int index;
        private ArrayVector vector;

        public iterator(ArrayVector a) {
            this.index = -1;
            this.vector = a;
        }

        public boolean hasNext() {
            if (index < vector.length() - 1) return true;
            else return false;


        }

        public Object next() throws NoSuchElementException {
            try {
                if (this.hasNext()) {
                    this.index++;
                    return vector.getElement(this.index);
                } else return null;
            } catch (NoSuchElementException e) {
                e.printStackTrace();
                return null;
            }
        }

        public void remove() {
        }
    }

    //@Override
    // StringBuffer - watch;
    public String toString() {
        String s="";
        for (int i = 0; i < vector.length; i++) {

            s+=vector[i]+" ";
        }
        return s;
    }

    public boolean equals(Object obj) {
        if (obj == null) return false;

        try {
            Vector vector = (ArrayVector) obj;
        } catch (ClassCastException e) {
            Vector vector = (LinkedListVector) obj;

            if (this.length() == vector.length()) {
                for (int i = 0; i < this.length(); i++) {
                    if (this.getElement(i) != vector.getElement(i)) return false;
                }
            }else return false;
        }
        return true;
    }

    public Object clone() {
        ArrayVector newVector = new ArrayVector(this.length());
        for (int i = 0; i < this.length(); i++) {
            newVector.setElement(i,this.getElement(i));
        }
        return newVector;
        //return  new LinkedListVector(1);
    }

     public int hashCode(){
        int hash = 0;
        for (int i = 0; i < this.length(); i++) {
            hash += Float.floatToIntBits(this.getElement(i)) ^ hash;
        }
        return hash;
    }

}
